﻿#if UNITY_EDITOR && UNITY_EDITOR_WIN
using System.IO;
using CC_Module.AssetsUplate;
using M3ExcelToConfig;
using UnityEditor;
using UnityEngine;

namespace G02 {
    /// <summary>
    /// 生成资源列表
    /// </summary>
    public class C_GenerateAssetsList  {

        static string gameIndexName = "/G02";

        [MenuItem("Tools/G02/资源更新/wwwServer生成资源列表")]
        static void S_GenerateAssetsList() {
            string webPath, sourcePath;
            System.Diagnostics.Stopwatch ww = new System.Diagnostics.Stopwatch();
            ww.Start();
            string sourcePathHead = Application.dataPath + "/../CommomExcelData/Config" + gameIndexName;
            FileInfo[] files = new DirectoryInfo(sourcePathHead).GetFiles("*.xlsx", SearchOption.AllDirectories);
            Excel2ConfigEditor.S_ReadData(files, false, sourcePathHead, sourcePathHead);

            string sourcePathHead2 = Application.dataPath + "/../CommomExcelData/Language" + gameIndexName;
            FileInfo[] files2 = new DirectoryInfo(sourcePathHead2).GetFiles("*.xlsx", SearchOption.AllDirectories);
            Excel2LanguageEditor.S_ReadData(files2,false);

            //生成资源路径数据
            //UI00.C_BuildAssetBundles_Editor.S_GenerateResourcesPathTxt(Application.dataPath + "/../CommomExcelData/ArtPaths/G01");

            webPath = "C:/AppServ/www/GameAssets/" + Application.identifier;
            string projectPath = Application.streamingAssetsPath;
            if (Directory.Exists(webPath)) {
                Directory.Delete(webPath, true);
            }
            sourcePath = Application.dataPath + "/../CommomExcelData";
            sourcePath = Path.GetFullPath(sourcePath);

            S_CopyFile(sourcePathHead, webPath + "/Config"+ gameIndexName, "*.txt");
            S_CopyFile(sourcePathHead2, webPath + "/Language" + gameIndexName, "*.txt");
            C_125_AssetsUpdateParts2.C_Tool.S_CreatAssetsConfigExe(webPath);
            S_CopyFile(webPath, projectPath, "*.*");
            string o_PersistencePath = Application.persistentDataPath + "/GameAssets";
            if (Directory.Exists(o_PersistencePath)) {
                Directory.Delete(o_PersistencePath, true);
            }
            ww.Stop();
            float useTime = ww.ElapsedMilliseconds / 1000f;
            EditorUtility.DisplayDialog("好消息,数据生成完成", string.Format("    消耗时间=={0}秒", useTime.ToString("0.000")), "我知道了");
            AssetDatabase.Refresh();
        }
        static void S_CopyFile(string sourcePath,string targetPath,string copyType) {
            sourcePath = new DirectoryInfo(sourcePath).FullName;
            Debug.LogFormat("路径____{0}____{1}", sourcePath, targetPath);
            FileInfo[] fileInfos= new DirectoryInfo(sourcePath).GetFiles(copyType, SearchOption.AllDirectories);
            foreach(var n in fileInfos) {
                string fullPath = targetPath + n.FullName.Remove(0, sourcePath.Length);
                Debug.LogFormat("拷贝____{0}___", fullPath);
                string dir = Path.GetDirectoryName(fullPath);
                if (Directory.Exists(dir) == false) {
                    Directory.CreateDirectory(dir);
                }
                byte[] bytes = File.ReadAllBytes(n.FullName);
                File.WriteAllBytes(fullPath, bytes);
            }
        }
    }
}
#endif